// 总的路由数据
const routes = [
    {
      name: "home",
    },
    {
      name: "banner",
      children: [
        {
          name: "banner-list",
          children: [
            {
              name: "banner-list-1",
            },
            {
              name: "banner-list-2",
            },
          ],
        },
        {
          name: "banner-add",
        },
      ],
    },
    {
      name: "manager",
      children: [
        {
          name: "manager-list",
        },
      ],
    },
  ];
  
  // 当前用户的路由权限
  const checkedKeys = ["banner", "banner-list", "banner-list-1"];


 /*  //只处理了第一层代码
  function getRoles(routes,roles){
    let result=[]
    routes.forEach((route) => {
        if(roles.includes(route.name)){
            result.push(route)
        }
        console.log(result);//{name: 'banner', children: Array(2)}
    });
    return result
  } */

  /* function getRoles(routes,roles){
    let result=[]
    routes.forEach((route) => {
        if(roles.includes(route.name)){
            //判断当前route是否有children
            if(route.children){
                let result1=[]
                route.children.forEach((child)=>{
                    if(roles.includes(child.name)){
                        result1.push(child)
                    }
                })
                //此处修改了原数据，因此需要对原数据拷贝
                route.children=result1//需要用下一代的result1覆盖父代的children
                console.log(result1);//{name: 'banner-list', children: Array(2)}
            }
            result.push(route)
        }
    });
    console.log(result)
    return result
    } */

    function getRoles(routes,roles){
        let result=[]
        routes.forEach((route) => {
            const obj={...route}
            if(roles.includes(obj.name)){
                //判断当前route是否有children
                if(obj.children){
                    obj.children=getRoles(obj.children,roles)
                }
                result.push(obj)
            }
        });
        console.log(result)//得到最终所需
        return result
        }


